package com.education.common.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.education.common.utils.RequestUtils;
import com.education.common.utils.Result;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @author 余一
 * @Description
 * @date 2021/7/19 16:31
 */
public abstract class BaseInterceptor implements HandlerInterceptor {
    private static final String contentType = "application/json; charset=utf-8";

    protected void renderJson(HttpServletResponse response, Result result){
        String dataJson = JSONObject.toJSONString(result);
        PrintWriter writer = null;
        try {
            response.setHeader("Pragma","no-cache");
            response.setHeader("Cache-Control","nocache");
            response.setDateHeader("Expires",0);
            response.setContentType(contentType);
            writer = response.getWriter();
            writer.write(dataJson);
            writer.flush();
            return;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    protected String getRequestUrl(HttpServletRequest request) {
        return RequestUtils.getRequestUrl(request);
    }

    /**
     * 获取json 参数值
     * @param request
     * @return
     */
    protected String readData(HttpServletRequest request) {
        return RequestUtils.readData(request);
    }
}
